package com.cory.boot.sys.mapper;

import java.io.Serializable;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import com.baomidou.mybatisplus.core.mapper.Mapper;
import com.cory.boot.sys.entity.SysDictValue;
import com.jarvis.cache.annotation.Cache;
import com.jarvis.cache.annotation.CacheDelete;
import com.jarvis.cache.annotation.CacheDeleteKey;


@Repository
public interface SysDictValueMapper extends Mapper<SysDictValue> {
    
    List<SysDictValue> list(SysDictValue param);
    
    @Select("select * from sys_dict_value where  state != 9 and  dict_code = #{dictCode} and dict_key = #{dictKey}")
    SysDictValue getDictValue(@Param("dictCode")String dictCode, @Param("dictKey")String dictKey);

    @Select("select dict_key from sys_dict_value where  state != 9 and  dict_code = #{dictCode} and dict_value = #{dictValue}")
    String getDictKey(@Param("dictCode")String dictCode, @Param("dictValue")String dictValue);

    @Update("update sys_dict_value set sort = #{sort} where dict_code = #{dictCode} and dict_key = #{dictKey}")
    void updateSort(@Param("dictCode")String dictCode, @Param("dictKey")String dictKey, @Param("sort")Integer sort);
    
	String CACHENAME = "SysDictValue:";
	int EXPIRETIME = 15 * 60;

    @Cache(expire = EXPIRETIME, key = "'" + CACHENAME + "'+#args[0]")
	@Select("select * from sys_dict_value where dict_code = #{dictCode} and state != 9 order by sort ")
	List<SysDictValue> selectByCode(@Param("dictCode")String dictCode);
    
    @CacheDelete({@CacheDeleteKey(value = "'" + CACHENAME + "'+#args[0].dictCode")})
    int insert(SysDictValue dict);
    
    @CacheDelete({@CacheDeleteKey(value = "'" + CACHENAME + "'+#args[0].dictCode")})
    int update(SysDictValue dict);
    
    @CacheDelete({@CacheDeleteKey(value = "'" + CACHENAME + "'+#args[0]")})
    void delete(@Param("dictCode")Serializable dictCode, @Param("dictKeys")List<String> dictKeys);
    
    @CacheDelete({@CacheDeleteKey(value = "'" + CACHENAME + "'+#args[0]")})
    int deleteByDictCode(@Param("dictCode")Serializable dictCode);
    
    @CacheDelete({@CacheDeleteKey(value = "'" + CACHENAME + "'+#args[0]")})
    @Update("update sys_dict_value set state = #{state} where dict_code = #{dictCode} and dict_key = #{dictKey}")
    void updateState(@Param("dictCode")String dictCode, @Param("dictKey")String dictKey, @Param("state")Integer state);

    @Select("<script>" +
            "select group_concat(dict_value separator '|') from sys_dict_value where state = 1 and dict_code = #{dictCode} and dict_key in " +
            "<foreach item='item' index='index' collection='dictKeyList' open='(' separator=', ' close=')'>" +
            "#{item}" +
            "</foreach>" +
            " group by dict_code" +
            "</script>")
    String selectValueConcatByKeys(@Param("dictCode")String code,@Param("dictKeyList") List<String> dictKeyList);

    @Select("<script>" +
            "select group_concat(dict_key) from sys_dict_value where state = 1 and  dict_code = #{dictCode} and dict_value in " +
            "<foreach item='item' index='index' collection='dictValueList' open='(' separator=', ' close=')'>" +
            "#{item}" +
            "</foreach>" +
            " group by dict_code" +
            "</script>")
    String selectKeyConcatByValues(@Param("dictCode")String code,@Param("dictValueList") List<String> dictValueList);
}
